﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Soufun.DB;
using Soufun.Model;
using System.Data;

namespace Soufun.DAL
{
    public class DProjectArea
    {
        public int Add(ProjectAreaInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("Insert into tProjectAreaInfo (");
            strSql.Append("ProjectId,BuildingID,UnitID,");
            strSql.Append("FloorID,ProjectAreaString)");
            strSql.Append(" value (");
            strSql.Append("@0,@1,@2,");
            strSql.Append("@3,@4)");
            object[] paramaters = new object[]
                {
                    model.ProjectId,
                    model.BuildingId,
                    model.UnitId,
                    model.FloorId,
                    model.ProjectAreaString
                };
            int result = DbHelper.ExecuteSql(strSql.ToString(), paramaters);            
            return result;
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder(); 
            strSql.Append("select t.Id,t.ProjectId,t.BuildingID,");
            strSql.Append("t.UnitID,t.FloorID,t.ProjectAreaString,");
            strSql.Append("tb.ProjectName ");
            strSql.Append("from tProjectAreaInfo t ");
            strSql.Append("left join tprojecturlinfo tb ");
            strSql.Append("on t.ProjectId = tb.Id ");            
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelper.Query(strSql.ToString());
        }

        public int DeleteByProjectId(int projectId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tProjectAreaInfo ");             
            strSql.Append("where ProjectId=");
            strSql.Append(projectId);             
            int result = DbHelper.ExecuteSql(strSql.ToString());
            return result;
        }

        public DataSet GetBuildingIdList(int projectId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select distinct BuildingID ");
            strSql.Append("from tProjectAreaInfo ");
            strSql.Append("where ProjectId = ");
            strSql.Append(projectId);
            return DbHelper.Query(strSql.ToString());
        }

    }
}
